#include <LoRaLib.h>
#include "loraAPP.h"
#include "Arduino.h"

// depends on LoRaLib

SX1278 lora = new LoRa(Pin15, Pin26, Pin25);

void InitLora()
{
  // Please set code:Serial.begin(9600)
  Serial.begin(9600);
  Serial.print("\n");
  for (unsigned char i = 0; i < 6; i++)
  {
    Serial.print("==========================");
  }

  Serial.println("\nStart To Initializing Lora Module!");
  int state = lora.begin();

  if (state == ERR_NONE)
  {
    Serial.println(F("Successfully Initialize Lora!"));
  }
  else
  {
    Serial.print(F("Failed To Initialize Lora"));
    Serial.println(state);
    while (true)
      ;
  }
}



void GetData()
{

  Serial.print(F("Waiting for incoming transmission ... "));

  String str;
  int state = lora.receive(str);

  if (state == ERR_NONE)
  {
    // packet was successfully received
    Serial.println(F("success!"));

    // print data of the packet
    Serial.print(F("Data:\t\t\t"));
    Serial.println(str);
    ledblink();
    // print RSSI (Received Signal Strength Indicator)
    // of the last received packet
    Serial.print(F("RSSI:\t\t\t"));
    Serial.print(lora.getRSSI());
    Serial.println(F(" dBm"));

    // print SNR (Signal-to-Noise Ratio)
    // of the last received packet
    Serial.print(F("SNR:\t\t\t"));
    Serial.print(lora.getSNR());
    Serial.println(F(" dB"));

    // print frequency error
    // of the last received packet
    Serial.print(F("Frequency error:\t"));
    Serial.print(lora.getFrequencyError());
    Serial.println(F(" Hz"));
  }
  else if (state == ERR_RX_TIMEOUT)
  {
    // timeout occurred while waiting for a packet
    Serial.println(F("timeout!"));
  }
  else if (state == ERR_CRC_MISMATCH)
  {
    // packet was received, but is malformed
    Serial.println(F("CRC error!"));
  }
}

void sendData()
{
  Serial.print(F("Sending packet ... "));

  // you can transmit C-string or Arduino string up to
  // 256 characters long
  // NOTE: transmit() is a blocking method!
  //       See example TransmitInterrupt for details
  //       on non-blocking transmission method.
  int state = lora.transmit("Hello Lora Moudle Harold");

  // you can also transmit byte array up to 256 bytes long
  /*
    size_t len = 8;
    byte byteArr[len] = {0x01, 0x23, 0x45, 0x56,
                         0x78, 0xAB, 0xCD, 0xEF};
    int state = lora.transmit(byteArr, len);
  */

  if (state == ERR_NONE)
  {
    // the packet was successfully transmitted
    Serial.println(F(" success!"));

    // print measured data rate
    ledblink();
    Serial.print(F("Datarate:\t"));
    Serial.print(lora.getDataRate());
    Serial.println(F(" bps"));
  }
  else if (state == ERR_PACKET_TOO_LONG)
  {
    // the supplied packet was longer than 256 bytes
    Serial.println(F(" too long!"));
  }
  else if (state == ERR_TX_TIMEOUT)
  {
    // timeout occurred while transmitting packet
    Serial.println(F(" timeout!"));
  }
  // wait a second before transmitting again
}

void ledblink()
{
  digitalWrite(21, 0);
  delay(200);
  digitalWrite(21, 1);
  delay(200);
}